
'use strict';

var path = require('path');
var webpack = require('webpack');
var y = require('yargs');
var argv = y.argv;

var env = process.env.NODE_ENV || 'dev';
var port = process.env.NODE_PORT || 3030;
var Debug = env != 'prd';
var publicPath = {
    'dev':'http://localhost:'+port+'/',
    'fat':'/example/mobile/',
    'uat':'/example/mobile/',
    'prd':'/mobile/'
};
// 服务器地址
const proxy = {
    'dev':'http://localhost:'+(port+1),
    'fat':'https://fat.example.com',
    'uat':'https://uat.com',
    'prd':'https://www.example.com'
};

var entries = ['./src/app.js'];
var plugins = [
    new webpack.DefinePlugin({
        'process.env'  : {
            'NODE_ENV' : JSON.stringify(env)
        },
        'NODE_ENV'     : env,
        '__DEV__'      : env === 'dev',
        '__FAT__'      : env === 'fat',
        '__UAT__'      : env === 'uat',
        '__PROD__'     : env === 'prd',
        '__TEST__'     : env === 'test',
        '__DEBUG__'    : env === 'dev' && !argv.no_debug,
        '__COVERAGE__' : !argv.watch && env === 'test',
        '__BASENAME__' : JSON.stringify(process.env.BASENAME || '')
    })
];
var loaders = [
    { test: /\.woff(\?.*)?$/,  loader: 'url?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=application/font-woff' },
    { test: /\.woff2(\?.*)?$/, loader: 'url?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=application/font-woff2' },
    { test: /\.otf(\?.*)?$/,   loader: 'file?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=font/opentype' },
    { test: /\.ttf(\?.*)?$/,   loader: 'url?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=application/octet-stream' },
    { test: /\.eot(\?.*)?$/,   loader: 'file?prefix=fonts/&name=[path][name].[ext]' },
    { test: /\.svg(\?.*)?$/,   loader: 'url?prefix=fonts/&name=[path][name].[ext]&limit=10000&mimetype=image/svg+xml' },
    { test: /\.(png|jpg|jpeg|gif)$/,    loader: 'url?limit=8192' }
];

if(Debug){
    entries.unshift('webpack/hot/only-dev-server');
    entries.unshift('webpack-dev-server/client?http://localhost:'+port);
    plugins.push(
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin()
    );
    loaders.push(
        {
            test: /\.js$/,
            loaders: ['react-hot', 'babel?cacheDirectory=true&presets[]=es2015&plugins[]=transform-runtime'],
            exclude: /node_modules/
        },
        {
            test: /\.css$/,
            loaders:['style','css?importLoaders=2&sourceMap','autoprefixer?browsers=last 2 version'],
            exclude: /node_modules/
        },
        {
            test: /\.scss$/,
            loaders: [
                "style",
                "css",
                "autoprefixer?browsers=last 2 version",
                "sass?outputStyle=expanded&sourceMap=true&sourceMapContents=true"
            ]
        }
    );
}else{
    plugins.push(
        new webpack.optimize.UglifyJsPlugin({
            compress: { warnings: false }
        })
    );
    loaders.push(
        {
            test: /\.(js|jsx)$/,
            loader: 'babel',
            exclude: /node_modules/,
            query: {
                cacheDirectory: true,
                plugins: ['transform-runtime'],
                presets: ['es2015', 'react', 'stage-0'],
                env: {
                  production: {
                    presets: ['react-optimize']
                  }
                }
            }
        },
        {
            test: /\.scss$/,
            loader: ExtractTextPlugin.extract("style", "css!autoprefixer?browsers=last 2 version!sass")
        }
    );
}

module.exports = {
    devtool: 'source-map',
    entry: entries,
    output: {
        path: path.resolve('./dist'),
        filename: '[name].bundle.js',
        publicPath: publicPath[env],
        proxy:proxy[env]
    },
    plugins: plugins,
    resolve: {
        extensions: ['', '.js','.jsx']
    },
    module: {
        loaders: loaders
    }
};
